package service;


import java.util.ArrayList;

import main.ThreadRunner;

import org.apache.log4j.Logger;

import crawler.CrawlerDocument;
import crawler.DocumentPool;

public class Allocator implements Runnable {
	
	private final Logger logger = Logger.getLogger(Allocator.class);
	
	private static int IDLE_MILLI_SECONDS = 60000;
	private DocumentPool urlPool = null;
	private ArrayList<CrawlerDocument> seedlist = new ArrayList<CrawlerDocument>();
	
	public Allocator(DocumentPool pool){
		this.urlPool = pool;
		IDLE_MILLI_SECONDS = Integer.parseInt(
				GlobalConfiguration.getInstance().get("allocator_idle"));
	}
	
	public void addSeed(CrawlerDocument seed){
		this.seedlist.add(seed);
	}
	
	public void addSeedlist(ArrayList<CrawlerDocument> list){
		this.seedlist.addAll(list);
	}

	@Override
	public void run() {
		
		logger.info("Allocator begings to run.");
		
		while(true){
			try {
				for (int i = 0; i < seedlist.size(); ++i){
					CrawlerDocument seed = seedlist.get(i);
					urlPool.put(seed);
					logger.debug("Put seed into blocking queue : " + seed);
				}
				Thread.sleep(Allocator.IDLE_MILLI_SECONDS);
			} catch (InterruptedException e) {
				System.out.println("Queue is blocking...");
			}
		}
	}

}
